home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1990 / number5 / xon_xoff.cpp < prev    next >
C/C++ Source or Header  |  1990-10-06  |  4KB  |  110 lines

  1. /*********************************************************************
  2. *   XON_XOFF.CPP - Listing 4
  3. *   Written by Kevin D. Weeks, August 1990
  4. *   Compiles and runs under Borland Turbo C++ and Zortech C++.
  5. */
  6. #include <stdio.h>
  7. #include "xon_xoff.hpp"
  8. // handshake function codes
  9. #define RECV_TEST   1
  10. #define SEND_TEST   2
  11. #define BUF_EMPTY   3
  12. #define XON         17
  13. #define XOFF        19
  14. int     xon_xoff_handler(int function_code);
  15. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  16. *   This method replaces Serial_Comm's open to allow for setting the
  17. *   hand-shaking function.                                          */
  18. Result  Xon_Xoff::open(void)
  19. {
  20.     // make sure no one else has the com port, then set hand-shaking
  21.     // and call Serial_Comm to open the port
  22.     if (open_flag == NULL) {
  23.         com_set_handshake(xon_xoff_handler);
  24.         return Serial_Comm::open();
  25.     }
  26.     return ERROR;
  27. }
  28. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  29. *   This method replaces Serial_Comm's close to allow for re-setting
  30. *   the hand-shaking function.                                      */
  31. void    Xon_Xoff::close(void)
  32. {
  33.     if (open_flag == this || open_flag == NULL) {
  34.         com_set_handshake(NULL);
  35.         Serial_Comm::close();
  36.     }
  37. }
  38. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  39. *   This is the function that actually performs hand-shaking. Although
  40. *   the assembly routines are coded to support calls to C functions,
  41. *   it is usually better practice to code this function in assembler
  42. *   so that it operates as fast as possible.
  43. *   The high byte of function_code contains the actual function code
  44. *   from the assembly routine.The low byte of the function code
  45. *   contains either the byte received or the byte to be sent.       */
  46. #pragma option -N-                          // turn off stack checking
  47. int     xon_xoff_handler(int function_code)
  48. {
  49.     // state flags
  50.     static Bool send_xoff = FALSE;
  51.     static Bool send_xon = FALSE;
  52.     static Bool sending_halted = FALSE;
  53.     static Bool xoff_sent = FALSE;
  54.     Comm_Status stat;
  55.     switch (function_code >> 8) {           // determine function code
  56.         case RECV_TEST:
  57.             stat.value = com_get_status();
  58.            // turn off receiving if there are less than 64 bytes still
  59.            // free in the receive buffer
  60.             if (com_chars_recvd() > 960)
  61.                 if (stat.flag.sending_message)
  62.                     send_xoff = TRUE;
  63.                 else {
  64.                     xoff_sent = TRUE;
  65.                     com_write_char(XOFF);
  66.                 }
  67.             if ((function_code & 0x00ff) == XOFF)
  68.                 // if XOFF received then stop sending
  69.                 if (stat.flag.sending_message) {
  70.                     com_stop_sending();
  71.                     sending_halted = TRUE;
  72.                     return 0xff00;
  73.                 }
  74.             if ((function_code & 0x00ff) == XON)
  75.                 // if XON received then start sending
  76.                 if (sending_halted) {
  77.                     com_start_sending();
  78.                     sending_halted = FALSE;
  79.                     return 0xff00;
  80.                 }
  81.             // clear high byte so the character will be saved
  82.             return (function_code & 0x00ff);
  83.         case SEND_TEST:
  84.             if (send_xon) {
  85.                 send_xon = FALSE;
  86.                 xoff_sent = FALSE;
  87.                 return (0xff00 | XON);
  88.             }
  89.             if (send_xoff) {
  90.                 send_xoff = FALSE;
  91.                 xoff_sent = TRUE;
  92.                 return (0xff00 | XOFF);
  93.             }
  94.             // clear high byte so send_tail will be incremented
  95.             return (function_code & 0x00ff);
  96.         case BUF_EMPTY:
  97.             stat.value = com_get_status();
  98.             if (xoff_sent)
  99.                 // if sending was stopped then restart it
  100.                 if (stat.flag.sending_message)
  101.                     send_xon = TRUE;
  102.                 else {
  103.                     com_write_char(XON);
  104.                     xoff_sent = FALSE;
  105.                 }
  106.             return 0;
  107.     };
  108. }
  109. #pragma option -N+                  // turn stack checking back on
  110.